//
//  DMCCycleImages.h
//  22-AliPlanet_HomeWork
//
//  Created by dllo on 16/8/4.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "ViewController.h"
#import "AliData.h"
#import "BigData.h"
#import "BigAction.h"
#import "LittleData.h"
#import "LittleAction.h"
#import "IconTableViewCell.h"
#import "TuijianTableViewCell.h"
#import "NewSongTableViewCell.h"
#import "DujiaTableViewCell.h"

#import "DMCCycleImages.h"

#define kAliPlanetURLString @"http://api.dongting.com/frontpage/frontpage?location=0&version=1470998222737&app=ttpod&v=v9.0.4.2016070616&user_id=0&mid=iPhone8%2C1&f=f320&s=s310&imsi=&hid=&splus=10.0&active=1&net=2&openudid=4bbd8925c4a3991ee51c9cd8b9a8dc5961820764&idfa=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&utdid=V62kivWPJBYDABK%2FL8gCUnDI&alf=(null)&bundle_id=com.ttpod.music&latitude=(null)&longtitude=(null)&access_token=(null)&vc=9000400"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet DMCCycleImages *cycleImagesView;
@property (nonatomic, strong) AliData *aliData;
@property (weak, nonatomic) IBOutlet UIView *playAndMusicView;
@property (weak, nonatomic) IBOutlet UILabel *zhiboLabel;
@property (weak, nonatomic) IBOutlet UILabel *musicLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *imagesArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [self requestDataWithURL:kAliPlanetURLString];
    [self config];
    
}

- (void)config {
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bar"]];
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bar"]];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"back"]];
    _playAndMusicView.layer.cornerRadius = 13;
    _playAndMusicView.layer.borderColor = [UIColor whiteColor].CGColor;
    _playAndMusicView.layer.borderWidth = 1;
    _tableView.tableHeaderView = _cycleImagesView;
}

- (void)requestDataWithURL:(NSString *)urlString {
    [super viewDidLoad];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *hotRequest = [NSMutableURLRequest requestWithURL:url];
    hotRequest.HTTPMethod = @"GET";
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:hotRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSError *error = nil;
            NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data
                                                                       options:NSJSONReadingMutableContainers
                                                                         error:&error];
            self.aliData = [AliData modelWithDictionary:dictionary];
            self.imagesArray = [NSMutableArray array];
            for (LittleData *data in _aliData.data[0].data) {
                [_imagesArray addObject:data.picUrl];
            }
            _cycleImagesView.array = _imagesArray;
            [self.tableView reloadData];
        });
    }];
    [dataTask resume];
}
#pragma mark - tableView协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _aliData.data.count - 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 6) {
        return _aliData.data[section + 1].data.count;
    } else {
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        IconTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"icon"];
        cell.data = _aliData.data[indexPath.section + 1].data;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    } else if (indexPath.section == 1) {
        TuijianTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tuijian"];
        cell.titleLabel.text = _aliData.data[indexPath.section + 1].name;
        cell.data = _aliData.data[indexPath.section + 1].data;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    } else if (indexPath.section == 6) {
        DujiaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dujia"];
        LittleData *littleData = _aliData.data[indexPath.section + 1].data[indexPath.row];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.data = littleData;
        return cell;
    } else {
        NewSongTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newsong"];
        cell.titleLabel.text = _aliData.data[indexPath.section + 1].name;
        cell.data = _aliData.data[indexPath.section + 1].data;
        if (cell.data.count > 3) {
            cell.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:(arc4random() % 10) / 10.0f];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.section_cell = indexPath.section + 1;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 60;
    } else if (indexPath.section == 1) {
        return 120;
    } else if (indexPath.section == 2 || indexPath.section == 5) {
        return 180;
    } else if (indexPath.section == 3 || indexPath.section == 4) {
        return 330;
    } else {
        tableView.separatorColor = [UIColor whiteColor];
        tableView.separatorInset = UIEdgeInsetsMake(5, 2, 2, 2);
        return 70;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 6) {
        
        NSLog(@"%ld", indexPath.row);
    } else if (indexPath.section == 0) {
        NSLog(@"%ld", indexPath.row);
    }
}
// 返回头视图的标题view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 6) {
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), 30)];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), 30)];
        CGFloat centerX = CGRectGetWidth(tableView.bounds) / 2;
        label.center = CGPointMake(centerX, 15);
        label.textAlignment = NSTextAlignmentCenter;
        label.text = _aliData.data[section + 1].name;
        label.textColor = [UIColor whiteColor];
        [backView addSubview:label];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(CGRectGetWidth(tableView.bounds) - 50, 0, 50, 30);
        [button setTitle:@"更多>" forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        [backView addSubview:button];
        backView.backgroundColor = [UIColor clearColor];
        return backView;

    } else {
        return nil;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 6) {
        return 30;
    } else {
        return 0;
    }
}

#pragma mark - Other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
